#include "CBaseEnemy.h"
#include "../CGame.h"
#include "../core/CDirect3D.h"
#include "../core/CCamera.h"
#include "../core/CRetroInput.h"
#include "CPlayerObject.h"
#include "CEnemySpawner.h"
#include "CObjectManager.h"

CBaseEnemy::CBaseEnemy(void)
{
	m_pOwnedSpawner = NULL;
}

CBaseEnemy::~CBaseEnemy(void)
{
}

void CBaseEnemy::Initialize()
{
	CBaseObject::Initialize();
}

void CBaseEnemy::Initialize(int posX, int posY, float depth, int collisionWidth, int collisionHeight, CPlayerObject* pTarget)
{
	CBaseObject::Initialize(posX, posY, depth, collisionWidth, collisionHeight);
	m_pPlayerTarget = pTarget;
}

void CBaseEnemy::Input(CRetroInput* pInput)
{
}

void CBaseEnemy::Update(float fDeltaTime)
{
	if(m_pPlayerTarget != NULL)
	{
		POINT playerPT = m_pPlayerTarget->GetPosition();
		vec3<float> direction;
		direction.x = playerPT.x - m_vfPosition.x;
		direction.y = playerPT.y - m_vfPosition.y;
		direction.z = 1.0f;
		direction.normalize();

		m_vfPosition.x += direction.x * 300 * fDeltaTime;
		m_vfPosition.y += direction.y * 300 * fDeltaTime;

	}
}

void CBaseEnemy::Render(CDirect3D* pRender, CCamera* pCamera)
{
	CBaseObject::Render(pRender, pCamera);
}

void CBaseEnemy::Destroy()
{
	if(m_pOwnedSpawner != NULL)
	{
		m_pOwnedSpawner->SubtractSpawn();
	}
}

void CBaseEnemy::SetSpawnOwner(CEnemySpawner* pOwner)
{
	m_pOwnedSpawner = pOwner;
}

bool CBaseEnemy::CheckCollision(IBaseObject* pObject)
{
	// my collision box
	RECT myBox;
	myBox.left		=	m_vfCollisionBox.left	+ m_vfPosition.x;
	myBox.top		=	m_vfCollisionBox.top	+ m_vfPosition.y;
	myBox.right		=	m_vfCollisionBox.right	+ m_vfPosition.x;
	myBox.bottom	=	m_vfCollisionBox.bottom	+ m_vfPosition.y;

	switch(pObject->Type())
	{
	case OBJECT_PLAYER:
	case OBJECT_ENEMY:
		{
			CBaseEnemy* pEnemy = (CBaseEnemy*)pObject;

			// enemy collision box
			const RECT& enemyBox = pEnemy->GetCollisionBox();
			const POINT& otherPos = pEnemy->GetPosition();
			RECT otherBox;
			memcpy(&otherBox, &enemyBox, sizeof(RECT));
			otherBox.left	+= otherPos.x;
			otherBox.top	+= otherPos.y;
			otherBox.right	+= otherPos.x;
			otherBox.bottom	+= otherPos.y;
			
			// check collision
			RECT overlap;
			BOOL colide = IntersectRect(&overlap, &myBox, &otherBox);
			if(colide)//abs(dx) <= w && abs(dy) <= h)
			{
				POINT dimentions;
				dimentions.x = overlap.right - overlap.left;
				dimentions.y = overlap.bottom - overlap.top;

				// determin the side
				float w = 0.5 * static_cast<float>((myBox.right - myBox.left) + (otherBox.right - otherBox.left));
				float h = 0.5 * static_cast<float>((myBox.bottom - myBox.top) + (otherBox.bottom - otherBox.top));
				float dx = m_vfPosition.x - otherPos.x;
				float dy = m_vfPosition.y - otherPos.y;
				float wy = w * dy;
				float hx = h * dx;
				
				if (wy > hx)
				{
					if (wy > -hx)
					{
						// On the top
						m_vfPosition.y += dimentions.y;
					}
					else
					{
						// on the left
						m_vfPosition.x -= dimentions.x;
					}
				}
				else
				{
					if (wy > -hx)
					{
						// on the right
						m_vfPosition.x += dimentions.x;
					}
					else
					{
						// at the bottom
						m_vfPosition.y -= dimentions.y;
					}

				}
				return true;
			}

		} break;
	case OBJECT_PROJECTILE:
		{
			CBaseEnemy* pEnemy = (CBaseEnemy*)pObject;

			// enemy collision box
			const RECT& enemyBox = pEnemy->GetCollisionBox();
			const POINT& otherPos = pEnemy->GetPosition();
			RECT otherBox;
			memcpy(&otherBox, &enemyBox, sizeof(RECT));
			otherBox.left	+= otherPos.x;
			otherBox.top	+= otherPos.y;
			otherBox.right	+= otherPos.x;
			otherBox.bottom	+= otherPos.y;
			
			// check collision
			RECT overlap;
			BOOL colide = IntersectRect(&overlap, &myBox, &otherBox);
			if(colide != 0)
			{
				// TODO: subtract health.  destroy for now
				m_pObjMan->RemoveObject((IBaseObject*)this);
				// remove the bullet
				m_pObjMan->RemoveObject(pObject);
			}
		} break;
	}
	return false;
}
